package com.sky.service.impl;

import com.sky.dto.ReportDto;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 查询销量排名top10
     *
     * @param dto
     * @return
     */
    @Override
    public SalesTop10ReportVO top10(ReportDto dto) {
        LocalDateTime begin = LocalDateTime.of(dto.getBegin(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(dto.getEnd(), LocalTime.MAX);
        //获取菜品及销量排行
        Map map = new HashMap();
        map.put("status",5);
        map.put("begin",begin);
        map.put("end",end);
        List<OrderDetail> details = reportMapper.top10(map);
        //取出菜品名和销量
        List<String> names = details.stream().map(detail -> detail.getName()).collect(Collectors.toList());
        List<Integer> numbers = details.stream().map(detail -> detail.getNumber()).collect(Collectors.toList());
        //包装并返回
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(names,","))
                .numberList(StringUtils.join(numbers,","))
                .build();
    }

    /**
     * 用户统计接口
     *
     * @param dto
     * @return
     */
    @Override
    public UserReportVO userStatistics(ReportDto dto) {
        //获取日期列表
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(dto.getBegin());
        while (!dto.getBegin().equals(dto.getEnd())){
            dto.setBegin(dto.getBegin().plusDays(1));
            dateList.add(dto.getBegin());
        }
        Integer totalUser = 0;
        List<Integer> totalUserList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();
        //获取新增用户数列表
        for (LocalDate localDate : dateList) {
            LocalDateTime begin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer newUser = reportMapper.countNewUser(begin,end);
            //获取总用户量列表
            totalUser += newUser;
            totalUserList.add(totalUser);
            newUserList.add(newUser);
        }
        //封装返回值
        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .build();
    }

    /**
     * 营业额统计
     *
     * @param dto
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(ReportDto dto) {
        //获取日期列表
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(dto.getBegin());
        while (!dto.getBegin().equals(dto.getEnd())){
            dto.setBegin(dto.getBegin().plusDays(1));
            dateList.add(dto.getBegin());
        }
        List<BigDecimal> turnoverList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime begin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin",begin);
            map.put("end",end);
            map.put("status", Orders.COMPLETED);
            BigDecimal turnover = reportMapper.getturnover(map);
            if(turnover == null){
                turnover = BigDecimal.ZERO;
            }
            turnoverList.add(turnover);
        }
        return TurnoverReportVO
                .builder()
                .turnoverList(StringUtils.join(turnoverList,","))
                .dateList(StringUtils.join(dateList,","))
                .build();
    }

    /**
     * 订单统计
     *
     * @param dto
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(ReportDto dto) {
        //获取日期列表
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(dto.getBegin());
        while (!dto.getBegin().equals(dto.getEnd())){
            dto.setBegin(dto.getBegin().plusDays(1));
            dateList.add(dto.getBegin());
        }
        //获取订单总数
        Integer totalOrders = 0;
        //获取有效订单数
        Integer completeOrders = 0;
        //获取每日订单数列表
        List<Integer> totalOrdersList = new ArrayList<>();
        //获取每日完成订单数列表
        List<Integer> completeOrdersList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime begin = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of(localDate, LocalTime.MAX);
            Map mapTotal = new HashMap();
            mapTotal.put("begin",begin);
            mapTotal.put("end",end);
            Integer total = reportMapper.countOrder(mapTotal);
            totalOrdersList.add(total);
            Map mapComplete = new HashMap();
            mapComplete.put("begin",begin);
            mapComplete.put("end",end);
            mapComplete.put("status", Orders.COMPLETED);
            Integer complete = reportMapper.countOrder(mapComplete);
            completeOrdersList.add(complete);
            totalOrders += total;
            completeOrders += complete;
        }
        Double ratio =  0.0;
        //计算完成率
        if(totalOrders!=0){
            ratio = completeOrders * 1.0 / totalOrders;
        }
        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCompletionRate(ratio)
                .orderCountList(StringUtils.join(totalOrdersList,","))
                .totalOrderCount(totalOrders)
                .validOrderCount(completeOrders)
                .validOrderCountList(StringUtils.join(completeOrdersList,","))
                .build();
    }


}
